package org.cb.input;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Random;

import org.cb.core.ColourBot;
import org.cb.core.Global;


/**
 * 
 * @author Unknown
 * 
 * Edited to work with the new layout.
 * 
 * Not tested.
 *
 */
public class Mouse {
	public boolean mouseMoving = false;
	private double mouseSpeed = 1.8;
	public Point mouseXY = new Point(100, 0);

	
	private int getRandom() throws InterruptedException{
        Random rand = new Random();
        //lag var 47-219, usu 78-94; currently tends low
        return rand.nextInt(100) + 40;
    }
	
	private void moveSpeed(Point Destination) throws InterruptedException{
		//PointerInfo PInfo = MouseInfo.getPointerInfo();
		System.out.println("Doing mouse move");
		Point origin = mouseXY;
		Point Difference = new Point((int)(Destination.getX() - mouseXY.getX()),(int)(Destination.getY() - mouseXY.getY()));				
		mouseMoving = true;
		for(double Current = 0; Current < 1; Current+= (mouseSpeed/Math.sqrt(Math.pow(Difference.getX(),2) + Math.pow(Difference.getY(),2)))){
			mouseXY = new Point((int)origin.getX() + (int)(Difference.getX() * Current), (int)origin.getY() + (int)(Difference.getY() * Current));
			silentMove(mouseXY.x, mouseXY.y);
			ColourBot.sleep(3);
		}
		mouseXY = new Point(Destination.x, Destination.y);
		silentMove(mouseXY.x, mouseXY.y);
		ColourBot.sleep(3);
		mouseMoving = false;
	}
	
	private void silentMove(int x, int y) throws InterruptedException{
		Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, x, y, 0, false, 0));
	}
	
	public void leftClick(int x, int y) throws InterruptedException{
		mouseMove(new Point(x, y));
		long lagTime = System.currentTimeMillis();
		Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_PRESSED, lagTime, MouseEvent.BUTTON1_MASK, x, y, 0, false, 1));
        lagTime += getRandom();
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_RELEASED, lagTime, MouseEvent.BUTTON1_MASK, x, y, 0, false, 1));
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_CLICKED, lagTime, MouseEvent.BUTTON1_MASK, x, y, 0, false, 1));
	}
	
	public void middleClick(int x, int y) throws InterruptedException{
		mouseMove(new Point(x, y));
		long lagTime = System.currentTimeMillis();
		Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_PRESSED, lagTime, MouseEvent.BUTTON2_MASK, x, y, 0, false, 1));
        lagTime += getRandom();
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_RELEASED, lagTime, MouseEvent.BUTTON2_MASK, x, y, 0, false, 1));
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_CLICKED, lagTime, MouseEvent.BUTTON2_MASK, x, y, 0, false, 1));
	}
	
	public void rightClick(int x, int y) throws InterruptedException{
		mouseMove(new Point(x, y));
		long lagTime = System.currentTimeMillis();
		Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_PRESSED, lagTime, MouseEvent.BUTTON3_MASK, x, y, 0, false, 1));
        lagTime += getRandom();
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_RELEASED, lagTime, MouseEvent.BUTTON3_MASK, x, y, 0, false, 1));
        Global.getTheCanvas().dispatchEvent(new MouseEvent(Global.getTheCanvas(), MouseEvent.MOUSE_CLICKED, lagTime, MouseEvent.BUTTON3_MASK, x, y, 0, false, 1));
	}
	
	public void mouseMove(Point destination) throws InterruptedException{
		moveSpeed(destination);
	}

	public double getMouseSpeed() {
		return mouseSpeed;
	}

	public void setMouseSpeed(double mouseSpeed) {
		this.mouseSpeed = mouseSpeed;
	}
}
